package com.example.zhyq.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.SearchBean
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 维修
 */
@Route(path = "/app/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    lateinit var repairAdapter: RepairAdapter
    var flag = false //false升序 true降序
    var isClick = false
    override fun initView() {

        repairAdapter = RepairAdapter()
        binding.rvRepair.adapter = repairAdapter
        binding.rvRepair.layoutManager = LinearLayoutManager(this)

        binding.headRepair.headerTitle.text = "维修管理"
        binding.headRepair.headerBack.setOnClickListener {
            finish()
        }
        binding.headRepair.apply {
            headerAdd.setImageResource(R.drawable.baseline_add_24)
                headerAdd.setOnClickListener {
                    ARouter.getInstance().build("/app/AddRepairActivity").navigation()
                }
        }

        //滑动删除
        val itemTouchHelper =
            ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {
                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
                    repairAdapter.data.removeAt(position)
                    repairAdapter.notifyItemRemoved(position)
                    vm.deleteRepair(repairAdapter.data[position].id!!)
                }

            })
        itemTouchHelper.attachToRecyclerView(binding.rvRepair)

        //点击进入维修详情
        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/RepairDetailActivity")
                .withString("rid", repairAdapter.data[position].id.toString()).navigation()
        }

        //点击排序
        binding.repairOrder.setOnClickListener {
            if (flag) {
                repairAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_downward_24)
            } else {
                repairAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_upward_24)
            }
            flag = !flag
            repairAdapter.notifyDataSetChanged()
        }


        vm.getSearch()
        binding.btnSearch.setOnClickListener {
            val query = binding.repairSv.text.toString()
            if (query != null) {
                if (isClick) {
                    vm.getRepairsByNo(query)
                }else {
                    vm.getRepairsByUname(query)
                }
            }
            vm.insertSearch(SearchBean(name = query!!))
        }

        binding.repairSv.addTextChangedListener { object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
               if (p0.isNullOrEmpty()) {
                   vm.getRepairs()
               }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        } }
        //搜索
//        binding.repairSv.setOnQueryTextListener(object : OnQueryTextListener {
//            override fun onQueryTextSubmit(query: String?): Boolean {
//                if (query != null) {
//                    if (isClick) {
//                        vm.getRepairsByNo(query)
//                    }
//                    vm.getRepairsByUname(query)
//                }
//
//                vm.insertSearch(SearchBean(name = query!!))
//                return true
//            }
//
//            override fun onQueryTextChange(newText: String?): Boolean {
//                return true
//            }
//
//        })

        //设置关闭监听器 关闭搜索框时触发
//        binding.repairSv.setOnCloseListener {
//            vm.getRepairs()
//            return@setOnCloseListener true
//        }

        //点击筛选 进行工号查询
        binding.repairFilter.setOnClickListener {
            isClick = true
        }
    }

    override fun initData() {
        vm.getRepairSuc.observe(this) {
            repairAdapter.data.clear()
            repairAdapter.data.addAll(it)
            repairAdapter.notifyDataSetChanged()
        }

        vm.getRepairByNoSuc.observe(this) {
            repairAdapter.data.clear()
            // repairAdapter.addData(it)
            repairAdapter.data.add(it)
            repairAdapter.notifyDataSetChanged()
        }


        vm.insertSearch.observe(this) {
            Log.i("===", it.toString())
            vm.getSearch()
        }

        vm.getSearchs.observe(this) {his->
            binding.flex.removeAllViews()
            his.forEach {bean->
                val view = LayoutInflater.from(this).inflate(R.layout.flex_history,null)
                view.findViewById<TextView>(R.id.flex_title).apply {
                    setText(bean.name)
                    setOnClickListener {
                        binding.repairSv.setText(bean.name)
                        vm.getRepairsByNo(bean.name)
                    }
                }

                binding.flex.addView(view)

                view.findViewById<ImageView>(R.id.flex_delete).setOnClickListener {
                    vm.deleteSearch(bean)
                }


            }
        }

        vm.deleteSearch.observe(this) {
            if (it>0) {
                vm.getSearch()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getRepairs()
    }
}